<template>
  <div>
    <QuestionItem
      v-for="item in topicList"
      :key="item.id"
      :question="item"
      :qIndex="1"
      :qType="item.question_type"
      :qScore="0"
      :answer="setCorrectAnswer(item)"
      :level="1"
    ></QuestionItem>
  </div>
</template>

<script>
import QuestionItem from './components/QuestionItem'
import { mapState } from 'vuex'
export default {
  components: {
    QuestionItem
  },
  data() {
    return {
      topicList: []
    }
  },
  computed: {
    ...mapState({
      topicCollected: state => state.exercise.topic_collected
    })
  },
  created() {
    this.getExamTopic(this.$route.query.id)
  },
  methods: {
    // 获取试题
    getExamTopic(id) {
      const _this = this
      _this.$api.getExamTopic(
        {
          topic_id: id,
          origin: 6
        },
        res => {
          _this.topicList = res.data
        }
      )
    },
    // 获取正确的答案
    setCorrectAnswer(item) {
      let answer
      if (this.$util.inArray(item.question_type, [1, 2, 3, 4])) {
        let topic = item.options.filter(item => item.is_right == 1)
        answer = topic.map(item => item.key)
      } else if (item.question_type == 5) {
        answer = item.options.map(item => item.values)
      } else if (item.question_type == 7) {
        // 完型填空合拼正确的试题数组
        let topic = item.topics.reduce(
          (pre, cur) => pre.concat(cur.options.filter(item => item.is_right == 1)),
          []
        )
        answer = topic.map(item => item.key)
      } else if (item.question_type == 8) {
        answer = [item.reference_answer]
      }
      return answer
    }
  }
}
</script>

<style></style>
